home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / ast_comp / gopher.lha / gopher1.01 / gopherd / error.c < prev    next >
C/C++ Source or Header  |  1992-05-01  |  7KB  |  384 lines

  1. /*
  2.  * Error handling routines from Stevens: UNIX network programming
  3.  * pp 722-731
  4.  */
  5.  
  6. /*
  7.  * Error handling routines.
  8.  *
  9.  * The functions in this file are independent of any application
  10.  * variables, and may be used in any C program.  Either of the names
  11.  * CLIENT of SERVER may be defined when compiling this function.
  12.  * If neither are defined, we assume SERVER.
  13.  */
  14.  
  15. #include "gopherd.h"
  16. #include <varargs.h>
  17.  
  18. #ifdef CLIENT
  19. #ifdef SERVER
  20. /*no way jose, can't define both! (CLIENT and SERVER)*/
  21. #endif
  22. #endif
  23.  
  24. #ifndef CLIENT
  25. #ifndef SERVER
  26. #define SERVER 1
  27. #endif
  28. #endif
  29.  
  30. #ifndef NULL
  31. #define NULL ((void *) 0)
  32. #endif
  33.  
  34. extern char *pname;
  35.  
  36. #ifdef CLIENT  /* these all output to stderr */
  37.  
  38. err_init()
  39. {
  40.     /* don't have to do anything */
  41. }
  42.  
  43. /*
  44.  * Fatal error.  Print a message and terminate.
  45.  * Don't dump core and don't print the system's errno value.
  46.  *
  47.  *     err_quit(str, arg1, arg2, .....)
  48.  *
  49.  * The string "str" must specify the conversion specification for any args.
  50.  *
  51.  */
  52.  
  53. /*VARARGS1*/
  54. err_quit(va_alist)
  55. va_dcl
  56. {
  57.     va_list args;
  58.     char *fmt;
  59.  
  60.     va_start(args);
  61.     if (pname != NULL)
  62.         fprintf(stderr, "%s: ",pname);
  63.     fmt = va_arg(args, char *);
  64.     vprintf(stderr, fmt, args);
  65.     fputc('\n', stderr);
  66.     va_end(args);
  67.  
  68.     exit(1);
  69. }
  70.  
  71. /*
  72.  * Fatal error related to a system call. Print the message and terminate.
  73.  * Don't dump core, but do print the systems errno value and its
  74.  * associated message.
  75.  *
  76.  *    err_sys(str, arg1, arg2, ...)
  77.  *
  78.  */
  79.  
  80. /*VARARGS1*/
  81. err_sys(va_alist)
  82. va_dcl
  83. {
  84.     va_list args;
  85.     char *fmt;
  86.  
  87.     va_start(args);
  88.     if (pname != NULL)
  89.         fprintf(stderr, "%s: ", pname);
  90.     fmt = va_arg(args, char *);
  91.     vprintf(stderr, fmt, args);
  92.     va_end(args);
  93.  
  94.     my_perror();
  95.  
  96.     exit(1);
  97. }
  98.  
  99.  
  100. /*
  101.  * Recoverable error. print a message and return to the caller.
  102.  *
  103.  *     err_ret(str, arg1, arg2, ...)
  104.  */
  105.  
  106. /*VARARGS1*/
  107. err_ret(va_alist)
  108. va_dcl
  109. {
  110.     va_list    args;
  111.     char    *fmt;
  112.  
  113.     va_start(args);
  114.     if (pname != NULL)
  115.         fprintf(stderr, "%s: ", pname);
  116.     fmt = va_arg(args, char *);
  117.     vprintf(stderr, fmt, args);
  118.     va_end(args);
  119.  
  120.     my_perror();
  121.  
  122.     fflush(stdout);
  123.     fflush(stderr);
  124.     return;
  125. }
  126.  
  127.  
  128. /*
  129.  * Fatal error.  Print a message, dump core and terminate.
  130.  *
  131.  *    err_dump(str, arg1, arg2, ....)
  132.  *
  133.  */
  134.  
  135. /*VARARGS1*/
  136. err_dump(va_alist)
  137. va_dcl
  138. {
  139.     va_list args;
  140.     char *fmt;
  141.  
  142.     va_start(args);
  143.     if (pname != NULL)
  144.         fprintf(stderr, "%s: ", pname);
  145.     fmt = va_arg(args, char *);
  146.     vprintf(stderr, fmt, args);
  147.     va_end(args);
  148.  
  149.     my_perror();
  150.  
  151.     fflush(stdout);        /* abort doesn't flush stdio buffers */
  152.     fflush(stderr);
  153.  
  154.     abort();        /* dump core and terminate */
  155.     exit(1);
  156. }
  157.  
  158. /*
  159.  * Print the UNIX errno value
  160.  */
  161.  
  162.  
  163. my_perror()
  164. {
  165.     char *sys_err_str();
  166.  
  167.     fprintf(stderr, " %s\n", sys_err_str());
  168. }
  169.  
  170. #endif /* CLIENT */
  171.  
  172. #ifdef SERVER
  173.  
  174. #ifndef NOSYSLOG
  175.  /*
  176.   * Under useful OS's, these server routines use the syslog(3) facility.
  177.   * They don't append a newline for example.
  178.   */
  179.  
  180. #include <syslog.h>
  181.  
  182.  
  183. #else /* no syslog() */
  184. /*
  185.  * There really ought to be a better way to handle server logging
  186.  * under System V
  187.  */
  188. #define syslog(a,b)    fprintf(stderr, "%s\n", (b))
  189. #define openlog(a,b,c)    fprintf(stderr, "%s\n", (a))
  190.  
  191. #endif  /* NOSYSLOG */
  192.  
  193. char emesgstr[255] = {0};  /* used by all server routines */
  194.  
  195. /*
  196.  * Identify ourselves, for syslog() messages.
  197.  *
  198.  * LOG_PID is an option that says prepend each message with our pid.
  199.  * LOG_CONS is an option that says write to the console if unable to
  200.  * send the message to syslogd.
  201.  * LOG_DAEMON is our facility.
  202.  */
  203.  
  204. err_init()
  205. {
  206.     /* have to be able to reopen, since daemon_start keeps closing us */
  207.     closelog();    /* in case we've been closed by daemon_start */
  208. #ifdef LOG_DAEMON
  209.     openlog("gopherd", (LOG_PID|LOG_CONS), LOG_DAEMON);
  210. #else
  211.     /* old old syslog - probably Ultrix */
  212.     openlog("gopherd", LOG_PID);
  213. #endif
  214. }
  215.  
  216. /*
  217.  * Fatal error.  Print a message and terminate.
  218.  * Don't print the system's errno value.
  219.  *
  220.  *    err_quit(str, arg1, arg2, ...)
  221.  */
  222.  
  223. /*VARARGS1*/
  224. err_quit(va_alist)
  225. va_dcl
  226. {
  227.     va_list args;
  228.     char *fmt;
  229.  
  230.     va_start(args);
  231.     fmt = va_arg(args, char *);
  232.     vsprintf(emesgstr, fmt, args);
  233.     va_end(args);
  234.  
  235.     syslog(LOG_ERR, emesgstr);
  236.     LOGGopher(-1, emesgstr);
  237.  
  238.     exit(1);
  239. }
  240.  
  241.  
  242. /*
  243.  * Fatal error related to a system call.  Print the message and terminate.
  244.  * Don't dump core, but do print the systems errno value and its associated
  245.  * message.
  246.  */
  247.  
  248. /*VARARGS*/
  249. err_sys(va_alist)
  250. va_dcl
  251. {
  252.     va_list args;
  253.     char *fmt;
  254.  
  255.     va_start(args);
  256.     fmt = va_arg(args, char *);
  257.     vsprintf(emesgstr, fmt, args);
  258.     va_end(args);
  259.  
  260.     my_perror();
  261.     syslog(LOG_ERR, emesgstr);
  262.     LOGGopher(-1, emesgstr);
  263.  
  264.     exit(1);
  265. }
  266.  
  267.  
  268. /*
  269.  * Recoverable error.  Print a message, and return to caller.
  270.  */
  271.  
  272. /*VARARGS1*/
  273. err_ret(va_alist)
  274. va_dcl
  275. {
  276.     va_list args;
  277.     char *fmt;
  278.  
  279.     va_start(args);
  280.     fmt = va_arg(args, char *);
  281.     vsprintf(emesgstr, fmt, args);
  282.     va_end(args);
  283.  
  284.     my_perror();
  285.     syslog(LOG_ERR, emesgstr);
  286.     LOGGopher(-1, emesgstr);
  287.  
  288.     return;
  289. }
  290.  
  291.  
  292. /*
  293.  * Fatal error.  Print a message, dump core and terminate.
  294.  */
  295.  
  296. /*VARARGS1*/
  297. err_dump(va_alist)
  298. va_dcl
  299. {
  300.     va_list args;
  301.     char *fmt;
  302.  
  303.     va_start(args);
  304.     fmt = va_arg(args, char *);
  305.     vsprintf(emesgstr, fmt, args);
  306.     va_end(args);
  307.  
  308.     my_perror();
  309.     syslog(LOG_ERR, emesgstr);
  310.     LOGGopher(-1, emesgstr);
  311.  
  312.     abort();
  313. }
  314.  
  315. /*
  316.  * Print the UNIX errno value.
  317.  * We just append it the the end of the emesgstr[] array.
  318.  */
  319.  
  320. my_perror()
  321. {
  322.     int len;
  323.     char *sys_err_str();
  324.  
  325.     len = strlen(emesgstr);
  326.     sprintf(emesgstr + len, " %s", sys_err_str());
  327. }
  328.  
  329. #endif  /* SERVER */
  330.  
  331.  
  332. extern int errno;        /* UNIX error number */
  333. extern int sys_nerr;        /* # of error message strings in sys table */
  334. extern char *sys_errlist[];    /* the system error message table */
  335.  
  336. #ifdef SYS5
  337. int t_errno;
  338. int t_nerr;
  339. char *t_errlist[1];
  340. #endif
  341.  
  342. /*
  343.  * Return a string containing some additional operating system
  344.  * dependent information.
  345.  * Note that different versions of UNIX assign different meanings
  346.  * to the same value of "errno".  This means that if an error
  347.  * condition is being sent ot another UNIX system, we must interpret
  348.  * the errno value on the system that generated the error, and not
  349.  * just send the decimal value of errno to the other system.
  350.  */
  351.  
  352. char *
  353. sys_err_str()
  354. {
  355.     static char msgstr[200];
  356.  
  357.     if (errno != 0) {
  358.         if (errno >0 && errno < sys_nerr)
  359.             sprintf(msgstr, "(%s)", sys_errlist[errno]);
  360.         else
  361.             sprintf(msgstr, "(errno = %d)", errno);
  362.     } else {
  363.         msgstr[0] = '\0';
  364.     }
  365.  
  366. #ifdef SYS5
  367.  
  368.     if (t_errno != 0) {
  369.         char tmsgstr[100];
  370.  
  371.         if (t_errno > 0 && t_errno < sys_nerr)
  372.             sprintf(tmsgstr, " (%s)", t_errlist[t_errno]);
  373.         else
  374.             sprintf(tmsgstr, ", (t_errno = %d)", t_errno);
  375.  
  376.         strcat(msgstr, tmsgstr);
  377.     }
  378. #endif
  379.  
  380.     return(msgstr);
  381. }
  382.  
  383.  
  384.